//
//  UserinfoView.m
//  Time
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "UserinfoView.h"

@interface UserinfoView ()
/**
 *  头像
 */
@property (nonatomic,retain) UIButton *headerButton;
/**
 *  用户名
 */
@property (nonatomic,retain) UILabel *unameLabel;
/**
 *  距今的时间
 */
@property (nonatomic,retain) UILabel *timeLabel;

@end


@implementation UserinfoView
-(void)dealloc{
    [_headerButton release];
    [_unameLabel release];
    [_timeLabel release];
    [_scbUserinfo release];
    [_pubdate release];
    [super dealloc];
}

-(instancetype)init{
    self = [super init];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
        
        self.headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.headerButton];
        [_headerButton release];
        
//        [self.headerButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(40);
//        }];
        
        self.unameLabel = [[UILabel alloc] init];
        [self addSubview:self.unameLabel];
        [_unameLabel release];
        
        self.timeLabel = [[UILabel alloc] init];
        [self addSubview:self.timeLabel];
        [_timeLabel release];
        
    }
    return self;
}
/**
 *  头像点击方法
 *
 *  @param button 参数就是按钮
 */
-(void)headerButtonAction:(UIButton *)button{
    NSLog(@"%@",self.scbUserinfo.uname);
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.headerButton.frame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
    
    self.headerButton.layer.masksToBounds = YES;
    self.headerButton.layer.cornerRadius = self.bounds.size.height * 0.5;
    self.headerButton.backgroundColor = [UIColor whiteColor];
    
    self.unameLabel.frame = CGRectMake(self.frame.size.height + 10, 20, self.frame.size.width - self.frame.size.height - 10, 20);
    self.unameLabel.textColor = [UIColor colorWithRed:153 / 255.0 green:204 / 255.0 blue:51 / 255.0 alpha:1.0];
    self.unameLabel.font = [UIFont systemFontOfSize:15.0];
    
    self.timeLabel.frame = CGRectMake(self.frame.size.height + 10, 0, self.frame.size.width - self.frame.size.height - 10, 20);
    self.timeLabel.font = [UIFont systemFontOfSize:12.0];
    self.timeLabel.textColor = [UIColor grayColor];
    
}

-(void)setScbUserinfo:(SCBUserinfo *)scbUserinfo{
    if (_scbUserinfo != scbUserinfo){
        [_scbUserinfo release];
        _scbUserinfo = [scbUserinfo retain];
    }

    [self.headerButton setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:scbUserinfo.icon] placeholderImage:nil];
    self.unameLabel.text = scbUserinfo.uname;
    
}

-(void)setPubdate:(NSNumber *)pubdate{
    if (_pubdate != pubdate) {
        [_pubdate release];
        _pubdate = [pubdate retain];
    }
    NSDate *date = [NSDate date];
    NSString *nowTime = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
    
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY/MM/dd"];
    
    NSDate *dateOfPub = [NSDate dateWithTimeIntervalSince1970:[pubdate integerValue]];
//    NSLog(@"%@",dateOfPub);
    
    NSString *dateOfPubStr = [formatter stringFromDate:dateOfPub];
//    NSLog(@"%@",dateOfPubStr);
    //现在与内容上线时的时间差
    NSInteger passTime = [nowTime integerValue] - [pubdate integerValue];
    
    if (passTime < 60) {
        self.timeLabel.text = @"刚刚";
    } else if (passTime >= 60 && passTime < 3600) {
        self.timeLabel.text = [NSString stringWithFormat:@"%ld分钟前",passTime / 60];
    } else if (passTime >= 3600 && passTime < 86400) {
        self.timeLabel.text = [NSString stringWithFormat:@"%ld小时前",passTime / 3600];
    } else {
        self.timeLabel.text = dateOfPubStr;
    }
    
    
}

@end
